﻿/// Author:					Joe Audette
/// Created:				2009-01-14
/// Last Modified:			2009-05-27
/// 
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using ud_Utilities;

namespace mojoPortal.Web.UI
{
    public partial class eStoreSelectorSetting : UserControl, ISettingControl
    {
        /// <summary>
        /// TinLT: "selectedValue is ModuleID to load".
        /// </summary>
        private string selectedValue = string.Empty;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (HttpContext.Current == null) { return; }
            Load += new EventHandler(Page_Load);
            EnsureItems();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDDLModules();
            }
        }

        private void EnsureItems()
        {
            //why is this null here, its declared
            if (ddlModule == null)
            {
                ddlModule = new DropDownList();
                if (Controls.Count == 0)
                {
                    Controls.Add(ddlModule);
                }
            }
        }

        private void GetSelectedItems()
        {
            selectedValue = string.Empty;
            selectedValue = ddlModule.SelectedValue;
            DataTable dt = Module.GetPageModulesTable(int.Parse(selectedValue));
            selectedValue += "-" + dt.Rows[0]["PageID"].ToString();
        }

        private void BindSelection()
        {
            if (!selectedValue.Equals(string.Empty))
            {
                BindDDLModules();
            }
        }

        private void BindDDLModules()
        {
            //Get Articles Module only
            SiteSettings settings = CacheHelper.GetCurrentSiteSettings();
            DBUtilities repository = new DBUtilities();
            ddlModule.DataSource = repository.GetModuleByModuleGuid(new Guid("699c9a81-3238-4b6b-840a-771efd8d3b63"), settings.SiteId);
            ddlModule.DataTextField = "ModuleTitle";
            ddlModule.DataValueField = "ModuleID";
            ddlModule.DataBind();
            if (ddlModule.Items.Count > 0)
            {
                if (!selectedValue.Equals(string.Empty))
                {
                    ListItem item = ddlModule.Items.FindByValue(selectedValue);
                    if (item != null)
                    {
                        ddlModule.Items.FindByValue(selectedValue).Selected = true;
                    }
                }
                else { ddlModule.SelectedIndex = 0; }
            }
        }

        #region ISettingControl

        public string GetValue()
        {
            EnsureItems();
            GetSelectedItems();
            return selectedValue;
        }

        public void SetValue(string val)
        {
            EnsureItems();
            selectedValue = val;
            BindSelection();
        }

        #endregion

    }
}